home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / ct_scan.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  5KB  |  195 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  CT_SCAN.CPP - Cubic Tetrahedron Scan Conversion Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. #include "ff_delta.h"
  39. #include "ff_scan.h"
  40. #include "ct_scan.h"
  41.  
  42. CubicScan::CubicScan()  // Class constructor
  43. {
  44.   int row;      // Loop index
  45.   int width;    // Scan line width
  46.   
  47.   status = TRUE;        // Initialize object status
  48.  
  49.   // Allocate edge list
  50.   if ((edge_list = new FormEdgeInfo[FF_ArrayRes]) != NULL)
  51.   {
  52.     // Allocate cell information buffer
  53.     if ((cell_buffer =
  54.         new (FormCellInfo (*[FF_ArrayRes]))) != NULL)
  55.     {
  56.       width = FF_ArrayRes;
  57.       for (row = 0; row < FF_ArrayRes; row++)
  58.       {
  59.         if ((cell_buffer[row] = new FormCellInfo[width])
  60.             == NULL)
  61.         {
  62.           // Release partially allocated buffer
  63.           row--;
  64.           for ( ; row >= 0; row--)
  65.             delete [] cell_buffer[row];
  66.           delete [] cell_buffer;
  67.  
  68.           // Release edge list memory 
  69.           delete [] edge_list;
  70.  
  71.           status = FALSE;
  72.           break;
  73.         }
  74.         width--;        // Decrement scan line width
  75.       }
  76.     }
  77.   }
  78.   else
  79.   {
  80.     delete [] edge_list;        // Release edge list memory
  81.     status = FALSE;
  82.   }
  83. }
  84.  
  85. CubicScan::~CubicScan()         // Class destructor
  86. {
  87.   int row;      // Loop index
  88.   
  89.   delete [] edge_list;          // Release edge list memory
  90.  
  91.   // Delete cell information buffer
  92.   for (row = 0; row < FF_ArrayRes; row++)
  93.     delete [] cell_buffer[row];
  94.   delete [] cell_buffer;
  95. }
  96.  
  97. // Initialize cell information buffer
  98. void CubicScan::InitBuffer()
  99. {
  100.   int row, col;     // Loop indices
  101.   int width;        // Scan line width
  102.  
  103.   width = FF_ArrayRes;
  104.   for (row = 0; row < FF_ArrayRes; row++)
  105.   {
  106.     for (col = 0; col < width; col++)
  107.     {
  108.       cell_buffer[row][col].depth = FF_Infinity;
  109.       cell_buffer[row][col].id = FF_None;
  110.     }
  111.     width--;    // Decrement scan line width
  112.   }
  113. }
  114.  
  115. void CubicScan::DrawEdgeList()  // Draw edge list
  116. {
  117.   int x, y;             // Loop indices
  118.   int sx, ex;           // Scan line x-axis co-ordinates
  119.   double dz;            // Pseudodepth delta
  120.   double iz;            // Element pseudodepth
  121.   double x_dist;        // X-axis distance
  122.   FormEdgeInfo *pedge;  // Edge info pointer
  123.   FormScanInfo *pss;    // Scan line start info pointer
  124.   FormScanInfo *pse;    // Scan line end info pointer
  125.   FormScanInfo *psw;    // Swap scan line info pointer
  126.  
  127.   pedge = &(edge_list[ymin]);
  128.   for (y = ymin; y < ymax; y++)
  129.   {
  130.     // Get scan line info pointers
  131.     pss = &(pedge->isect[0]);
  132.     pse = &(pedge->isect[1]);
  133.  
  134.     if (pss->x > pse->x)
  135.     {
  136.       // Swap scan line info pointers
  137.       psw = pss; pss = pse; pse = psw;
  138.     }
  139.  
  140.     // Get scan line x-axis co-ordinates
  141.     sx = min((int) pss->x, FF_ArrayRes - y);
  142.     ex = min((int) pse->x, FF_ArrayRes - y);
  143.  
  144.     if (sx < ex)        // Ignore zero-length segments
  145.     {
  146.       // Determine scan line start info
  147.       iz = pss->z;
  148.  
  149.       // Determine inverse slopes
  150.       x_dist = pse->x - pss->x;
  151.  
  152.       dz = (pse->z - iz) / x_dist;
  153.  
  154.       // Enter scan line
  155.       for (x = sx; x < ex; x++)
  156.       {
  157.         // Check element visibility
  158.         if (iz < (double) cell_buffer[y][x].depth)
  159.         {
  160.           // Update Z-buffer
  161.           cell_buffer[y][x].depth = (float) iz;
  162.  
  163.           // Set polygon identifier
  164.           cell_buffer[y][x].id = poly_id;
  165.         }
  166.  
  167.         // Update element pseudodepth
  168.         iz += dz;
  169.       }
  170.     }
  171.     pedge++;    // Point to next edge list element
  172.   }
  173. }
  174.  
  175. // Sum delta form factors
  176. void CubicScan::SumDeltas( float *ff_array )
  177. {
  178.   WORD poly_id;     // Polygon identifier
  179.   int row, col;     // Face cell indices
  180.   int width;        // Scan line width
  181.  
  182.   width = FF_ArrayRes;
  183.   for (row = 0; row < FF_ArrayRes; row++)
  184.   {
  185.     for (col = 0; col < width; col++)
  186.     {
  187.       if ((poly_id = cell_buffer[row][col].id) !=
  188.           FF_None)
  189.         ff_array[poly_id - 1] += dff.GetFactor(row, col);
  190.     }
  191.     width--;    // Decrement scan line width
  192.   }
  193. }
  194.  
  195.